
/**
 * XmlHandler.h
 * @date {DATE}
 * @author
 */
#ifndef XMLHANDLER_H
#define XMLHANDLER_H
#define BUFFSIZE    8192

#include <cstdio>
#include <string>
#include <list>
#include <proto/expat.h>
#include <proto/dos.h>
#include <proto/uni.h>
#include <proto/codesets.h>
#include <proto/exec.h>

#include "ARexxXmlException.h"

#ifdef DEBUG
#define XML_H_DEBUG printf
#define XML_H_TRACE(x, y...) {}
#else
#define XML_H_DEBUG(x, y...) {}
#define XML_H_TRACE(x, y...) {}
#endif

//extern struct ExecIFace *IExec;
//
// Xml Handlers
//
extern void start(void *data, const XML_Char *el, const XML_Char **attr);
extern void end(void *data, const XML_Char *el);
extern void charData(void *data, const XML_Char *el, int len);

class XmlEntity {
    public:
    XmlEntity();

    void SetId(const char * id);
    void SetValue(const char * value);
    ~XmlEntity ();
    /**
     * GetChild with name id and the index:th occurence.
     */
    XmlEntity * GetChild(std::string id, int index);
    /**
     * GetAttribute with name id and the index:th occurence.
     */
    XmlEntity * GetAttribute(std::string id);
    /**
     * GetChild with name id and the index:th occurence.
     */
    int GetNumOfChildren(std::string id);

    std::string _id;
    std::string *_value;
    std::list<XmlEntity*> *_children;
    std::list<XmlEntity*> *_attributes;
    XmlEntity * _parent;
};

class XmlHandler {
public:
    XmlHandler();
    ~XmlHandler();

    void ReadFromStdin();
    void ReadFromFile(STRPTR fileName, STRPTR xmlencoding, STRPTR destencoding);
    const char * GetTag(const char * tag);
    void DisposeDocument();
    static std::string _stemString;
    static XmlEntity * _currentEntity;
    static int _depth;
protected:
    XmlEntity *_document;
    const char* ConvertStringToLatin1(std::string);
private:
    char *_buff;
    XML_Parser _parser;
    struct Library *_codesetsBase;
    struct Library *_uniBase;
    struct CodesetsIFace *_iCodesets;
    struct UniIFace *_iUni;
    struct Hook *_mapCharHook;
    struct codeset *_srcCodeset;
    struct codeset *_dstCodeset;
};
extern unsigned int LatinToUTF8MapChar( REG(a0, struct Hook *h), REG(a2, APTR obj), REG(a1, unsigned int lchar));

#endif
